<?php
/*******************************************************************************
 * $Date:: Jan 20, 2011 4:37:49 PM                                              $
 * $Revision:                                                                  $
 * $Author:: Tuan Duong                                                        $
 * $Comment::                                                                  $
 ******************************************************************************/ 
/**
 * Description here...
 * 
 * @package Lua
 * @subpackage ##Subpackage##
 * @author Tuan Duong <bacduong[at]gmail[dot]com>
 * @version 
 */

class Lua_Grid_Column_Input extends Lua_Grid_Column_Abstract
{
    const CHECKBOX = 'checkbox';
    const RADIO = 'radio';
    
    private $type = 'text';
    private $checkAll = false;
    public function __construct($type, $key, $params = array())
    {
        $this->type = $type;
        parent::__construct($key, $params);
        $this->sortable = false;
    }
    
    public function renderCell($value = '', $row = array())
    {
        $buildMethod = 'build' . ucfirst($this->type);
        return parent::cell($this->$buildMethod($value, $row));
    }
    
    private function buildCheckbox($value, $row = array())
    {
        $inputHtml = '<input '; 
        $inputHtml .= ' type="' . $this->type . '"';
        $inputHtml .= ' name="' . $this->key . '[]"';
        $inputHtml .= ' id="' . $this->key . '"';
        $inputHtml .= ' value="' . $value . '"';
        //$inputHtml .= $this->callOptionChecked($row);
                
        foreach ($this->options as $key => $param) {
        	if ($key != 'onclick' && $key != 'changeCallback') {
                $inputHtml .= ' ' . $key . '="' . $row[$param] . '"';
            } else if(isset($this->options['changeCallback']) 
                && is_callable($this->options['changeCallback']) 
                && call_user_func($this->options['changeCallback'], $row)) {
                $inputHtml .= ' checked = "checked" ';
            }
            if (isset($this->options['onclick'])) {
                $inputHtml .= ' ' . $key . '="' . $param . '"';
            }
        }
        $inputHtml .= '"  />';
        return $inputHtml;
    }
    
    public function renderHeader(Zend_View $view, $nowrap = true)
    {
        if ($this->checkAll) {
            $headerHtml = '<td><input type="checkbox" onclick="$(\'input[name=' . $this->key . ']\').attr(\'checked\', this.checked)"/></td>';
            return $headerHtml;
        } else {
            return parent::renderHeader($view, $nowrap);
        }
    }
    
    /**
     * @param bool $checkAll
     */
    public function setCheckAll($checkAll)
    {
        if (self::CHECKBOX == $this->type) {
            $this->checkAll = $checkAll;
        }
    }
}